package com.icongyou.enterprise.talent_pool.service;

import com.icongyou.enterprise.data_aggregation.entity.AssignmentEntity;
import com.icongyou.enterprise.data_aggregation.service.DataAggregationService;
import com.icongyou.enterprise.talent_pool.mapper.AssignmentTagMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Slf4j
@Service
@RequiredArgsConstructor
public class AssignmentWeightService {

    private final AssignmentTagMapper assignmentTagMapper;
    private final DataAggregationService dataAggregationService;

    /**
     * 计算任务重要性权重
     */
    public Double calculateAssignmentImportanceWeight(String assignmentId) {
        try {
            // 获取任务实体
            AssignmentEntity assignment = dataAggregationService.getAssignmentInfo(assignmentId);
            if (assignment == null) {
                return 1.0; // 默认权重
            }

            String courseId = assignment.getCourseId();
            BigDecimal fullScoreBd = assignment.getFullScore();
            double fullScore = fullScoreBd != null ? fullScoreBd.doubleValue() : 0.0;

            if (courseId == null || fullScore <= 0) {
                return 1.0;
            }

            // 获取课程总分
            Double courseTotalScore = assignmentTagMapper.getCourseTotalScore(courseId);
            if (courseTotalScore == null || courseTotalScore <= 0) {
                return 1.0;
            }

            // 计算任务分数占比
            double scoreRatio = fullScore / courseTotalScore;

            // 标准化到0.3-1.0范围，重要任务权重更高
            return 0.3 + (scoreRatio * 0.7);

        } catch (Exception e) {
            return 1.0;
        }
    }
}